<?php
if( (!isset($_SESSION['next'])) || ($_SESSION['next'] < 2) )
{
	echo '<script laguage="javascript"> 
              window.location.href = "../installation/";
          </script>';
}
//status = 0: khong co loi, 1: co loi
$status = 0;
$message = '';
$host     = '';
$username = '';
$password = '';
$dbname   = '';
if(isset($_POST['host']))
{
	$host     = trim($_POST['host']);
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$dbname   = trim($_POST['dbname']);
	if(($host == '') || ($username == '') || ($dbname == ''))
	{
		$status = 1;
		$message = "Nhập thiếu thông tin";
	}
	
	include_once 'functions/Database.php';
	
	if($status == 0)
	{
		$db = new Database($host, "", $username, $password);
		if($db->getPDO() != null)
		{
			$db->createDatabase($dbname);
			$db1 = new Database($host, $dbname, $username, $password);
			$fileSql = INSTALL_PATH.'/sql/bkloud.sql';
			$content = file_get_contents($fileSql);
			$db1->importSql($content);
			
			$file_page_config = INSTALL_PATH.'/files/page_config.php';
			$fileConfig   = SITE_PATH.'/application/configs/config.php';
			$config = file_get_contents($file_page_config);
			$config = preg_replace("/(\n\\$)config\[\'database\'\]\[\'host\'\](.*)[^\n]/", "\n$"."config['database']['host'] = '$host';",$config);
			$config = preg_replace("/(\n\\$)config\[\'database\'\]\[\'username\'\](.*)[^\n]/", "\n$"."config['database']['username'] = '$username';",$config);
			$config = preg_replace("/(\n\\$)config\[\'database\'\]\[\'password\'\](.*)[^\n]/", "\n$"."config['database']['password'] = '$password';",$config);
			$config = preg_replace("/(\n\\$)config\[\'database\'\]\[\'dbname\'\](.*)[^\n]/", "\n$"."config['database']['dbname'] = '$dbname';",$config);
			$config = trim($config);
       	 	file_put_contents($fileConfig,$config);
       	 	
			$_SESSION['next'] = 3;
			echo '<script laguage="javascript"> 
             		 window.location.href = "../installation/?frame=account";
          		  </script>';
		}
		else 
			{
				$status = 1;
				$message = "Kết nối cơ sở dữ liệu thất bại.";
			}
	}
}
?>

       <script>
        	addActive('connect');
        	
        </script>
    	<div id="topright">
        	<div id="topright1">
        		<span> Kết nối cơ sở dữ liệu </span> 
            </div>
            <div id="topright2">
            	<button id="next" onclick="document.forms['connectDb'].submit()">
                </button>
            </div> 
    	</div> 
        
    	<div id="botright">
			<div id="botright1">
				  <form id="connectDb" action="../installation/?frame=connect" method="post">
     				 <?php
        				if($status == 1)
        				{
      				?>      
      				<div style="padding-bottom:15px; color:#FF0000;width:400px;font-weight:bold;">
      				<img src="images/error.png" width="32" height="32" style="float:left; margin-right:10px;"/>
      				Lỗi : <?php echo $message;?>
      				</div>
      				<?php
       				}
      				?>
			      	<p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Máy chủ CSDL</label>
			        	<input type="text" name="host" style="padding: 5px;width: 300px;" value="<?php echo $host;?>"/>
			       </p>
			       <p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Tài khoản</label>
			        	<input type="text" name="username" style="padding: 5px;width: 300px;" value="<?php echo $username;?>"/>
			       </p>
			       <p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Mật khẩu</label>
			        	<input type="password" name="password" style="padding: 5px;width: 300px;" value="<?php echo $password;?>"/>
			       </p>
			       <p  style="padding: 10px; margin: 0px;">
			        	<label style="width:200px;float:left;font-weight: bold;padding-top: 5px;">Tên CSDL</label>
			        	<input type="text" name="dbname" style="padding: 5px;width: 300px;" value="<?php echo $dbname;?>"/>
			       </p>
			     </form> 
			</div>       
			<div id="botright2">
			  <p>Các trường thông tin đều là bắt buộc</p>
			  <p>Cơ sở dữ liệu nếu đã tồn tại sẽ bị xóa và tạo lại mới</p>
			</div>
    	</div>